package com.example.smartcity1.service.patient;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity1.R;
import com.example.smartcity1.adapter.MyOrderAdapter;
import com.example.smartcity1.bean.NewsTypeBean;
import com.example.smartcity1.bean.PatientClassListBean;
import com.example.smartcity1.my.MyUserOrderActivity;
import com.example.smartcity1.network.RetrofitClient;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PatientClassListActivity extends AppCompatActivity {

    private RadioGroup didGroup;
    private RadioButton did1Radio;
    private RadioButton did2Radio;
    private ListView listView;
    private String token;
    private String patientName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_class_list);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle("门诊科室分诊");
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        SharedPreferences user = getSharedPreferences("user", MODE_PRIVATE);
        token = user.getString("token", "");
        Intent intent = getIntent();
        patientName = intent.getStringExtra("patientName");
        initView();
        did1Radio.setClickable(false);
        did2Radio.setClickable(false);
        didGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.did_1_radio:
                    setListData(1);
                    break;
                case R.id.did_2_radio:
                    setListData(2);
            }
        });
        setListData(1);
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }
    private void initView() {
        didGroup = findViewById(R.id.did_group);
        did1Radio = findViewById(R.id.did_1_radio);
        did2Radio = findViewById(R.id.did_2_radio);
        listView = findViewById(R.id.listView);
    }
    private void setListData(int did){
        RetrofitClient.serviceService().getPatientClassListData(token,did).enqueue(new Callback<PatientClassListBean>() {
            @Override
            public void onResponse(Call<PatientClassListBean> call, Response<PatientClassListBean> response) {
                PatientClassListBean body = response.body();
                if (body != null && body.getCode() == 200) {
                    List<PatientClassListBean.RowsDTO> rows = body.getRows();
                    List<String> data = new ArrayList<>();
                    for (PatientClassListBean.RowsDTO i : rows) {
                        data.add(i.getCategoryName());
                    }
                    listView.setAdapter(new ArrayAdapter<String>(PatientClassListActivity.this,android.R.layout.simple_list_item_1,data));
                    listView.setOnItemClickListener((parent, view, position, id) -> {
                        Intent intent = new Intent(PatientClassListActivity.this,PatientClassPageActivity.class);
                        intent.putExtra("patientName",patientName);
                        intent.putExtra("categoryName",rows.get(position).getCategoryName());
                        intent.putExtra("divisionId",rows.get(position).getId());
                        intent.putExtra("did",rows.get(position).getDid());
                        intent.putExtra("money",rows.get(position).getMoney());
                        startActivity(intent);
                    });
                    did1Radio.setClickable(true);
                    did2Radio.setClickable(true);
                }
            }
            @Override
            public void onFailure(Call<PatientClassListBean> call, Throwable t) {

            }
        });
    }
}